package org.example.gateway.controller;

import org.example.gateway.pojo.ChangeStatusDto;
import org.example.gateway.pojo.dto.RegisterAppDto;
import org.example.gateway.pojo.dto.UnregisterDto;
import org.example.gateway.pojo.vo.AppVo;
import org.example.gateway.pojo.vo.CommonResult;
import org.example.gateway.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/app")
public class AppController {

    @Autowired
    private AppService appService;

    @PostMapping("/register")
    public void register(@RequestBody @Validated RegisterAppDto dto) {
        appService.register(dto);
    }

    @PostMapping("/unregister")
    public void unregister(@RequestBody UnregisterDto dto) {
        appService.unregister(dto);
    }

    @GetMapping("/list")
    public ModelAndView list(ModelMap model) {
        List<AppVo> appVoList = appService.getList();
        model.put("appVOList", appVoList);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/applist.html");
        modelAndView.addAllObjects(model);
        return modelAndView;
    }

    @PutMapping("/status")
    public CommonResult updateEnabled(@RequestBody ChangeStatusDto dto) {
        appService.updateEnabled(dto);
        return CommonResult.success();
    }

    @DeleteMapping("/{id}")
    public CommonResult delete(@PathVariable("id") Integer id) {
        appService.delete(id);
        return CommonResult.success();
    }

    @GetMapping("/all")
    public CommonResult<List<AppVo>> getAppList() {
        return CommonResult.success(appService.getList());
    }
}
